package org.example.springboot.dao.impl;

import org.example.springboot.dao.Receiving_addressDAO;
import org.example.springboot.entity.Receiving_address;
import org.example.springboot.util.JDBCUtils;

import java.util.List;

public class Receiving_addressDAOImpl implements Receiving_addressDAO {
    @Override
    public List<Receiving_address> receiving_addressLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Receiving_address.class,"select * from s_receiving_address limit ?,?",
                start,rows);
    }

    @Override
    public Integer selectreceiving_addressCount() {
        return JDBCUtils.executeCount("select count(*) from s_receiving_address ");
    }

    @Override
    public List<Receiving_address> setAll() {
        List<Receiving_address> receiving_addresses = JDBCUtils.executeQuery(Receiving_address.class,"select * from s_receiving_address");
        return receiving_addresses;
    }

    @Override
    public Receiving_address selectById(String id) {
        List<Receiving_address> receiving_addresses = JDBCUtils.executeQuery(Receiving_address.class,"select * from s_receiving_address where id = ?",id);
        if (receiving_addresses.isEmpty()){
            return null;
        }else {
            return receiving_addresses.get(0);
        }
    }

    @Override
    public boolean inser(Receiving_address receiving_address) {
        int rows = JDBCUtils.executeUpdate("insert into s_receiving_address(id,receiving_address,receiving_person,mobile_phone,user_id,is_default)values(?,?,?,?,?,?)",
                receiving_address.getId(),receiving_address.getReceiving_address(),receiving_address.getReceiving_person(),receiving_address.getMobile_phone(),receiving_address.getUser_id(),receiving_address.getIs_default());
        return rows > 0;
    }

    @Override
    public boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_receiving_address where id = ? ",id);
        return rows > 0;
    }

    @Override
    public boolean update(Receiving_address receiving_address) {
        int rows = JDBCUtils.executeUpdate("update s_receiving_address set receiving_address = ?,receiving_person = ?,mobile_phone = ?,user_id = ?, is_default = ? where id = ?",
                receiving_address.getReceiving_address(),receiving_address.getReceiving_person(),receiving_address.getMobile_phone(),receiving_address.getUser_id(),receiving_address.getIs_default(),receiving_address.getId());
        return rows > 0;
    }
}
